/**
 * Baijiahulian.com Inc. Copyright (c) 2014-2016 All Rights Reserved.
 */
package com.baijia.authentication.principal.populator;

import com.baijia.authentication.Authentication;
import com.baijia.authentication.AuthenticationMetaDataPopulator;
import com.baijia.authentication.principal.Credentials;
import com.baijia.authentication.principal.credentials.MobileCredentials;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @title UserMetaDataPopulator
 * @desc TODO
 * @author macrohuang
 * @date Jan 20, 2016
 * @version 1.0
 */
@Component
public class MobileMetaDataPopulator implements AuthenticationMetaDataPopulator {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    // private static final String USER = "user";
    //
    // @Autowired
    // private UserDao userDao;
    // @Autowired
    // private OrgAccountDao orgAccountDao;

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.baijia.authentication.AuthenticationMetaDataPopulator#populateAttributes(com.baijia.authentication.Authentication
     * , com.baijia.authentication.principal.Credentials)
     */
    @Override
    public Authentication populateAttributes(Authentication authentication, Credentials credentials) {
        MobileCredentials userCredentials = (MobileCredentials) credentials;
        String mobile = userCredentials.getUsername();

        logger.debug("Try to populate user username:{}", mobile);
        authentication.getAttributes().put(ACCOUNT_MOBILE, credentials.getUsername());
        // User user = null;
        // try{
        // user= userDao.getUserByMobile(mobile);
        // authentication.getAttributes().put(ACCOUNT_ID, user.getId());
        // authentication.getAttributes().put(ACCOUNT_NUMBER, user.getNumber());
        // authentication.getAttributes().put(ACCOUNT_MIBILE, user.getMobile());
        // user.setPassword(null);
        // authentication.getAttributes().put(USER, JacksonUtil.obj2Str(user));
        // }catch(Exception e){
        // try{
        //
        // }
        // OrgAccount orgAccount = orgAccountDao.findByMobile(mobile, null);
        // authentication.getAttributes().put(ACCOUNT_ID, orgAccount.getId());
        // authentication.getAttributes().put(ACCOUNT_NUMBER, orgAccount.getNumber());
        // authentication.getAttributes().put(ACCOUNT_MIBILE, orgAccount.getMobile());
        // }
        // if (user == null){
        // }else{
        // }
        // try {
        // } catch (IOException e) {
        // logger.error("save user into dession error!", e);
        // }
        return authentication;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.baijia.authentication.AuthenticationMetaDataPopulator#supports(com.baijia.authentication.principal.Credentials
     * )
     */
    @Override
    public boolean supports(Credentials credentials) {
        return credentials instanceof MobileCredentials;
    }
}
